package org.example.cloudapply.controller;

import com.github.pagehelper.PageInfo;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.example.cloudapply.dto.TemplateCreateDTO;
import org.example.cloudapply.entity.CloudType;
import org.example.cloudapply.entity.InstanceTemplate;
import org.example.cloudapply.entity.Result;
import org.example.cloudapply.service.TemplateService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// TemplateController.java
@RestController
@RequestMapping("/api/templates")
@RequiredArgsConstructor
public class TemplateController {
    private final TemplateService templateService;

    @GetMapping
    public Result<List<InstanceTemplate>> getTemplates(
            @RequestParam CloudType cloudType) {
        return Result.success(templateService.getTemplates(cloudType));
    }

    @PostMapping
    public Result<?> createTemplate(@Valid @RequestBody TemplateCreateDTO dto) {
        System.out.println("aaaaaaa");
        templateService.createTemplate(dto);
        return Result.success();
    }

    @DeleteMapping("/{templateId}")
    public Result<?> deleteTemplate(@PathVariable Integer templateId) {
        templateService.deleteTemplate(templateId);
        return Result.success();
    }
    @GetMapping("/page")
    public Result<PageInfo<InstanceTemplate>> getTemplatesByPage(
            @RequestParam(required = false) CloudType cloudType,  // 修改为可选参数
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        return Result.success(templateService.getTemplatesByPage(cloudType, page, size));
    }


    @GetMapping("/all")
    public Result<PageInfo<InstanceTemplate>> getTemplatesAll(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        return Result.success(templateService.getTemplatesAll(page, size));
    }
}